<template>
  <div class="container">
    <div class="main">
      <div class="header">
        <div>说明：目前支持学科和关键字条件筛选</div>
        <div>
          <el-button
            type="success"
            icon="el-icon-edit"
            @click="$router.push('/questions/new')"
          >
            新增试题
          </el-button>
        </div>
      </div>
      <div class="inputs">
        <subject-seach
          @search="getList"
          :pageSize="currentSize"
          :page="currentPage"
          ref="seach"
        />
      </div>
      <el-alert :title="infoTip" type="info" show-icon> </el-alert>
      <el-table :data="list" style="width: 100%" :header-cell-style="headClass">
        <el-table-column prop="number" label="试题编号" width="120">
        </el-table-column>
        <el-table-column prop="subject" label="学科"> </el-table-column>
        <el-table-column
          prop="catalog"
          label="目录"
          width="80"
        ></el-table-column>
        <el-table-column prop="questionType" label="题型" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.questionType == 1">单选</span>
            <span v-else-if="scope.row.questionType == 2">多选</span>
            <span v-else>简答</span>
          </template>
        </el-table-column>
        <el-table-column prop="question" label="题干" width="280">
          <template slot-scope="scope">
            <span v-html="scope.row.question"></span>
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="180">
          <template slot-scope="scope">
            {{ parseTime(scope.row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column label="难度" prop="difficulty" width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.difficulty == 1">简单</span>
            <span v-else-if="scope.row.difficulty == 2">一般</span>
            <span v-else>困难</span>
          </template>
        </el-table-column>
        <el-table-column
          label="录入人"
          prop="creator"
          width="180"
        ></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              circle
              plain
              type="primary"
              size="small"
              icon="el-icon-view"
              @click="showQuestionView(scope.row.id)"
            ></el-button>
            <el-button
              circle
              plain
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="editQuestion(scope.row.id)"
            ></el-button>
            <el-button
              circle
              plain
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="deleteQuestion(scope.row.id)"
            ></el-button>
            <el-button
              circle
              plain
              type="warning"
              size="small"
              icon="el-icon-edit"
              @click="addStar(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next, sizes, jumper"
          :total="counts"
          :page-size="currentSize"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
    <question-preview
      :visible.sync="questionViewVisible"
      ref="questionPreview"
    ></question-preview>
  </div>
</template>

<script>
import subjectSeach from '../components/subject-seach.vue'
import questionPreview from '../components/question-preview'
import { parseTime } from '@/utils'
import { remove as removeQuestion, choiceAdd } from '@/api/hmmm/questions'

export default {
  name: 'questions',
  components: {
    subjectSeach,
    questionPreview
  },
  data () {
    return {
      // 页面配置
      counts: 0,
      pages: 0,
      list: [],
      currentPage: 1,
      currentSize: 5,
      // 预览弹出对话框控制
      questionViewVisible: false
    }
  },
  computed: {
    infoTip () {
      return `数据一共 ${this.counts} 条`
    },
    // 设置表头颜色
    headClass () {
      return { 'background-color': '#F8F8F9' }
    }
  },
  methods: {
    // 拿到子组件的值
    getList (value) {
      this.list = value.items
      this.counts = value.counts
      this.pages = value.pages
    },
    // 时间格式化
    parseTime (time) {
      return parseTime(new Date(time))
    },
    // 预览题目
    showQuestionView (id) {
      this.questionViewVisible = true
      this.$refs.questionPreview.getQuestionDetail(id)
    },
    // 编辑题目
    editQuestion (id) {
      this.$router.push(`/questions/new?id=${id}`)
    },
    // 删除基础题目
    deleteQuestion (id) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          removeQuestion({ id: id }).then((res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          this.list = this.list.filter((item) => {
            return item.id !== id
          })
        })
        .catch(() => {})
    },
    // 添加至精选
    addStar (id) {
      this.$confirm('此操作将添加该题目为精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      })
        .then(async () => {
          await choiceAdd({ id: id, choiceState: 1 })
          this.$message({
            type: 'success',
            message: '加入精选成功!'
          })
          this.$router.push('/questions/choice')
        })
        .catch(() => {})
    },
    // 分页更改
    handleCurrentChange (val) {
      this.currentPage = val
      this.$refs.seach.search(this.currentPage, this.currentSize)
    },
    handleSizeChange (val) {
      this.currentSize = val
      this.currentPage = 1
      this.$refs.seach.search(this.currentPage, this.currentSize)
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;
  .main {
    background-color: #fff;
    padding: 20px;
    box-shadow:2px 2px 2px #ccc;
    border-radius: 5px;
    .header {
      display: flex;
      justify-content: space-between;
      margin-bottom: 15px;
      div:first-child {
        font-size: 12px;
        color: pink;
      }
    }
    .el-alert {
      margin-bottom: 15px;
    }
    .pagination {
      display: flex;
      justify-content: flex-end;
      margin-top: 20px;
    }
  }
}
</style>
